﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SQLToolbox.View.Types;
using SQLToolbox.Controller;
using System.Windows.Threading;
using SQLToolbox.View.UserControls;
namespace SQLToolbox.View
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class MainWindow : Window, ISQLToolboxMainWindow
	{
		ISQLToolboxController controller;

		public MainWindow(ISQLToolboxController controller)
		{
			InitializeComponent();
			this.controller = controller;
		}

		private void fileExit(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		private void Compare_Click(object sender, RoutedEventArgs e)
		{

			controller.StartSchemaComparator();
		}
		public IProgressTab AddTab(object header, string tooltip)
		{
			SQLToolboxToolTabItem tab =AddClosableTab(header);
			tab.ToolTip = tooltip;
			return tab;
		}

		public IProgressTab AddTab(object header)
		{
			return AddClosableTab(header);
		}

		private SQLToolboxToolTabItem AddClosableTab(object header)
		{
			SQLToolboxToolTabItem ti = new SQLToolboxToolTabItem();
			ti.CloseTab += ClosableTabItem_CloseTab;

			ti.Header = header;
			int index = tcMain.Items.Add(ti);
			tcMain.SelectedIndex = index;
			ti.ContentChanged += new RoutedEventHandler(ti_ContentChanged);

			return ti;
		}

		void ti_ContentChanged(object sender, RoutedEventArgs e)
		{
			if (tcMain.SelectedItem.Equals(sender))
			{
				SQLToolboxToolTabItem ti = sender as SQLToolboxToolTabItem;
				if (ti == null)
					return;
				ArrangeToolbars();
			}

		}

		public void ArrangeToolbars()
		{
			if ((ActiveTab != null) && (ActiveTab.IsComparator))
			{
				tbtcmi_comparison.IsChecked = true;
				//tbComparerActions.Visibility = Visibility.Visible;
			}
			else
			{
				tbtcmi_comparison.IsChecked = false;
			}

		}

		public SQLToolboxToolTabItem ActiveTab
		{
			get
			{
				return tcMain.SelectedItem as SQLToolboxToolTabItem;
			}
		}

		private void ClosableTabItem_CloseTab(object sender, RoutedEventArgs e)
		{
			TabItem tabItem = e.Source as TabItem;
			CloseTab(tabItem);
		}

		private void CloseTab(TabItem t)
		{
			if (t != null)
			{
				tcMain.Items.Remove(t);
			}
		}

		private void CloseTabMenuItem_Click(object sender, RoutedEventArgs e)
		{
			TabItem tabItem = tcMain.SelectedItem as TabItem;
			CloseTab(tabItem);
		}

		private void CloseAllTabs_Click(object sender, RoutedEventArgs e)
		{
			tcMain.Items.Clear();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			var splash = new Splash();
			splash.ShowDialog();

		}

		private void tbtcmi_CheckedChanged(object sender, RoutedEventArgs e)
		{
			MenuItem mi = sender as MenuItem;
			if (mi == null)
				return;

			ToolBar t = null;
			switch (mi.Name)
			{
				case "tbtcmi_tools":
					t = tbTools;
					break;
				case "tbtcmi_comparison":
					t = tbComparerActions;
					break;
			}
			if (t == null)
				return;

			if (mi.IsChecked)
				t.Visibility = Visibility.Visible;
			else
				t.Visibility = Visibility.Collapsed;
		}

		private void tcMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			ArrangeToolbars();
		}

		private void Swap_Click(object sender, RoutedEventArgs e)
		{
			if ((ActiveTab != null)&& (ActiveTab.Content != null))
			{
				IComparisonToolbarTarget ct = ActiveTab.Content as IComparisonToolbarTarget;
				if (ct != null)
					ct.Swap();
			}
		}

		private void Refresh_Click(object sender, RoutedEventArgs e)
		{
			if ((ActiveTab != null) && (ActiveTab.Content != null))
			{
				IComparisonToolbarTarget ct = ActiveTab.Content as IComparisonToolbarTarget;
				if (ct != null)
					ct.Refresh();
			}
		}

        private void MatchRTL_Click(object sender, RoutedEventArgs e)
        {
            if ((ActiveTab != null) && (ActiveTab.Content != null))
            {
                IComparisonToolbarTarget ct = ActiveTab.Content as IComparisonToolbarTarget;
                if (ct != null)
                    ct.MatchRTL();
            }
        }

        private void MatchLTR_Click(object sender, RoutedEventArgs e)
        {
            if ((ActiveTab != null) && (ActiveTab.Content != null))
            {
                IComparisonToolbarTarget ct = ActiveTab.Content as IComparisonToolbarTarget;
                if (ct != null)
                    ct.MatchLTR();
            }
        }


	}
}
